-- Don't load anything unless Titan and ItemRack are both loaded
if not IsAddOnLoaded("Titan") or not IsAddOnLoaded("ItemRack") then return end

TITAN_ITEMRACK_ID = "ItemRack";
TITAN_ITEMRACK_VERSION = "2.1";
TITAN_ITEMRACK_ICON  = "Interface\\AddOns\\TitanItemRack\\Textures\\TitanItemRack"

TitanItemRack = {};

-- event handlers section
function TitanItemRack.OnLoad()


	    -- register the plugin
	    this.registry = {
	        id = TITAN_ITEMRACK_ID,
	        menuText = TITAN_ITEMRACK_ID,
	        buttonTextFunction = "TitanItemRack_GetButtonText",
	        tooltipTitle = TITAN_ITEMRACK_TOOLTIP_TITLE,
	        tooltipTextFunction = "TitanItemRack_GetTooltipText",
	        icon = TITAN_ITEMRACK_ICON,
	        iconWidth = 16,
	        category = "Interface",
	        version = TITAN_ITEMRACK_VERSION,
	        savedVariables = {
	            ShowIcon = 1,
	            ShowLabelText = TITAN_NIL,
	            ShowColoredText = 1,
	            OpenOnMouseover = 1
	        }
	    };
	this:RegisterEvent("PLAYER_ENTERING_WORLD");
	this:RegisterEvent("UNIT_INVENTORY_CHANGED");
end


function TitanItemRack.Event()
 if ((event == "UNIT_INVENTORY_CHANGED") and (arg1 == "player")) or event == "PLAYER_ENTERING_WORLD" then
   TitanPanelButton_UpdateButton(TITAN_ITEMRACK_ID);
 end
end

function TitanItemRack.DockWindows()
	local position = TitanPanelGetVar("Position");
	local i = TitanPanel_GetButtonNumber(TITAN_ITEMRACK_ID);
	local button = getglobal("TitanPanel"..TITAN_ITEMRACK_ID.."Button");
	if (TitanPanelSettings.Location[i] == "Bar") then 
		if position == TITAN_PANEL_PLACE_TOP then
			ItemRack.DockWindows("TOPLEFT", button, "BOTTOMLEFT", "VERTICAL");
		else
			ItemRack.DockWindows("BOTTOMLEFT", button, "TOPLEFT", "VERTICAL");
		end
	else
		ItemRack.DockWindows("BOTTOMLEFT", button, "TOPLEFT", "VERTICAL");
	end
end


function TitanItemRack.OnClick(self, button)
    -- show the ItemRack menu on left clicks
	if (button == "LeftButton") then
	
		if TitanGetVar(TITAN_ITEMRACK_ID, "OpenOnMouseover") then
		else
   		
		-- toggle menu
			if ItemRackMenuFrame:IsVisible() then
				ItemRackMenuFrame:Hide();
			else			  
				TitanItemRack.DockWindows();
				ItemRack.BuildMenu(20);
				ItemRackMenuFrame:SetFrameStrata("FULLSCREEN");
			end
		
		end
		
    end
	
    TitanPanelButton_OnClick(button);
	
end

function TitanItemRack.OnEnter()
   if (TitanGetVar(TITAN_ITEMRACK_ID, "OpenOnMouseover")) then   	
    TitanItemRack.DockWindows();
		ItemRack.BuildMenu(20)
		ItemRackMenuFrame:SetFrameStrata("FULLSCREEN");
   else
		TitanPanelButton_OnEnter()
   end
end


-- functions section
function TitanItemRack.GetButtonText(id)

    local labelText = nil;
    local outfitText = nil;

    -- display the label if the user has selected
    if (TitanGetVar(TITAN_ITEMRACK_ID, "ShowLabelText")) then
        labelText = TITAN_ITEMRACK_BUTTON_TEXT;
    end
    
    -- get any active outfits
    --ItemRack.IsSetEquipped(setname)
    
	local i =1;
  local usersets = ItemRackUser.Sets;
   
    
	 for i in next,usersets do
		if  not string.find(i, "~") then -- skip special sets (ItemRack-Queue and ItemRack-Normal)
			if ItemRack.IsSetEquipped(i) then
			     
               if (outfitText == nil) then
                  outfitText = i;
               else
                  outfitText = outfitText..", "..i;
               end
            
			end
		end
	end
      
   if (outfitText == nil) then
   
       -- we aren't wearing an outfit
       outfitText = TITAN_ITEMRACK_NO_OUTFIT;
   end
   
   if TitanGetVar(TITAN_ITEMRACK_ID, "ShowColoredText") then
   	outfitText = TitanUtils_GetGreenText(outfitText);
   else
    outfitText = TitanUtils_GetHighlightText(outfitText);
   end
   
   return labelText, outfitText;
end
function TitanItemRack.GetTooltipText()
    return TITAN_ITEMRACK_TOOLTIP_TEXT;
end


-- Titan Panel right click menu
function TitanPanelRightClickMenu_PrepareItemRackMenu()
   
    TitanPanelRightClickMenu_AddTitle(TitanPlugins[TITAN_ITEMRACK_ID].menuText);
    
    -- show the "Open selection on mouseover" option
    TitanPanelRightClickMenu_AddToggleVar(TITAN_ITEMRACK_MENU_OPEN_ON_MOUSEOVER, TITAN_ITEMRACK_ID, "OpenOnMouseover");
        
    -- show "options"
    info = {};
		info.text = TITAN_ITEMRACK_MENU_OPTIONS;
		info.value = TITAN_ITEMRACK_MENU_OPTIONS;
		info.func = function()
		ItemRack.ToggleOptions(1);
		end
		UIDropDownMenu_AddButton(info);    
    
    -- standard titan menu stuff
    TitanPanelRightClickMenu_AddSpacer();

    TitanPanelRightClickMenu_AddToggleIcon(TITAN_ITEMRACK_ID);
    TitanPanelRightClickMenu_AddToggleLabelText(TITAN_ITEMRACK_ID);
    TitanPanelRightClickMenu_AddToggleColoredText(TITAN_ITEMRACK_ID);

    TitanPanelRightClickMenu_AddSpacer();

    TitanPanelRightClickMenu_AddCommand(TITAN_PANEL_MENU_HIDE, TITAN_ITEMRACK_ID, TITAN_PANEL_MENU_FUNC_HIDE);

end



-- aliases for function pointers
TitanItemRack_GetButtonText = TitanItemRack.GetButtonText;
TitanItemRack_GetTooltipText = TitanItemRack.GetTooltipText;